package com.zeta.eduservice.controller.Front;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zeta.commonutils.R;
import com.zeta.eduservice.entity.question.EduFolder;
import com.zeta.eduservice.entity.question.EduQuestionAndFolder;
import com.zeta.eduservice.entity.vo.front.folderListVo;
import com.zeta.eduservice.service.EduFolderService;
import com.zeta.eduservice.service.EduQuestionAndFolderService;

/**
 * <p>
 * 文件夹 前端控制器
 * </p>
 *
 * @author zeta
 * @since 2020-08-24
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu_folder")
public class EduFolderController {

	@Autowired
	EduFolderService folderService;
	@Autowired
	EduQuestionAndFolderService qAfService;
	
	@PostMapping("")
	public R add(@RequestBody EduFolder eduFolder) {
		boolean save = folderService.save(eduFolder);
		QueryWrapper<EduFolder> queryWrapper=new QueryWrapper<EduFolder>();
		
		if(save)
			return R.ok();
		else
			return R.error();
	}
	@DeleteMapping("{id}")
	public R delete(@PathVariable String id) {
		boolean removeById = folderService.removeById(id);
		QueryWrapper<EduQuestionAndFolder> queryWrapper=new QueryWrapper<EduQuestionAndFolder>();
		queryWrapper.eq("folder_id", id);
		boolean remove = qAfService.remove(queryWrapper);
		if(removeById&&remove)
			return R.ok();
		else 
			return R.error();
	}
	@PutMapping("")
	public R update(@RequestBody EduFolder eduFolder) {
		folderService.updateById(eduFolder);
		return R.ok();
	}
	@GetMapping("{teacherid}")
	public R getAllList(@PathVariable String teacherid) {
		
		QueryWrapper<EduFolder> queryWrapper=new QueryWrapper<EduFolder>();
		queryWrapper.eq("teacher_id", teacherid);
		List<EduFolder> list=folderService.list(queryWrapper);
		return R.ok().data("folderList",list);
	}
	@PostMapping("deletefolders")
	public R deletefolders(@RequestBody String list) {
		JSONObject parseObject = JSONObject.parseObject(list);
		 String string = parseObject.getString(list);
		List<String> parseArray = JSONObject.parseArray(string,String.class);
		folderService.removeByIds(parseArray);
		return R.ok();
	}
}

